package com.ly.sy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.sy.entity.*;
import com.ly.sy.service.CourseService;
import com.ly.sy.service.SyuserService;
import com.ly.sy.service.XktableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Controller
@RequestMapping("/sy/xktable")
public class XktableController {

    @Autowired
    private XktableService xktableService;
    @Autowired
    private SyuserService syuserService;
    @Autowired
    private CourseService courseService;


//    获取用户的选课信息用来改变按钮
    @GetMapping("/userBycourse")
    public String userBycourse(@RequestParam Integer courseId ,HttpSession session){
        session.removeAttribute("coursexk");
        Syuser user = (Syuser) session.getAttribute("User");
        if(ObjectUtils.isEmpty(user)){
            return "User/details";
        }
        QueryWrapper<Xktable> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getUserId());
        wrapper.eq("course_id",courseId);
        List<Xktable> xklist = xktableService.list(wrapper);
        System.out.println("用户的选课信息:"+xklist);
        if(ObjectUtils.isEmpty(xklist)){
            return "User/details";
        }
        session.setAttribute("coursexk",xklist);

        return "User/details";

    }
    /**
     * 显示我加入的课程
     *
     * showmyclass()
     * **/
    @GetMapping("showmyclass")
    public String showmyclass(HttpSession session){
        Syuser user = (Syuser) session.getAttribute("User");
        session.removeAttribute("courseidList");
        QueryWrapper<Xktable> wrapper = new QueryWrapper<>();
        wrapper.select("course_id").eq("user_id",user.getUserId());
        List<Xktable> xklist = xktableService.list(wrapper);
        System.out.println("返回的数据:"+xklist);
        if(ObjectUtils.isEmpty(xklist)){
            session.setAttribute("userscourse",null);
            return "User/myclass";
        }else {

            session.setAttribute("courseidList",xklist);

            return "redirect:/sy/course/findusercourse";
        }

    }
//    用户退出课程
    @GetMapping("deletmycourse")
    @ResponseBody
    public String deletmycourse(@RequestParam Integer courseId ,HttpSession session){
        Syuser user = (Syuser) session.getAttribute("User");
        QueryWrapper<Xktable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.eq("course_id",courseId);
        boolean  rset=xktableService.remove(queryWrapper);
        System.out.println("========================操作状态=========================");
        System.out.println(rset);
        return "已退出该课程";
    }
//用户参加课程
    @GetMapping("joincourse")
    @ResponseBody
    public String joincourse(HttpSession session){
        Syuser user = (Syuser) session.getAttribute("User");
        Course course = (Course) session.getAttribute("acourse");
        Xktable xktable = new Xktable();
        xktable.setCourseId(course.getCourseId());
        xktable.setUserId(user.getUserId());
        xktableService.save(xktable);
        return "ok";
    }


//    ************************************************admin*************************************************
    //选课管理
    @GetMapping("adminxk")
    public String adminxk(HttpSession session){
        List<Xktable> xktables = xktableService.list(null);
        if (!ObjectUtils.isEmpty(xktables)) {
            List<Xktableadmin> xktableadmins = new ArrayList<>();
            for (Xktable xk : xktables) {
                Syuser syuser = syuserService.getById(xk.getUserId());
                Course course = courseService.getById(xk.getCourseId());
                Xktableadmin xktableadmin = new Xktableadmin();
                xktableadmin.setCourseId(course.getCourseId());
                xktableadmin.setUserId(syuser.getUserId());
                xktableadmin.setUserName(syuser.getUserName());
                xktableadmin.setCourseName(course.getCourseName());
                xktableadmin.setCourseDetails(course.getCourseDetails());
                xktableadmins.add(xktableadmin);
            }
            session.setAttribute("xkList",xktableadmins);
        }
        return "/admin/xklist";

    }
    //删除选课对象
    @PostMapping("deletexkObject")
    @ResponseBody
    public String deletexkObject(@RequestParam Integer userid, @RequestParam Integer couresid){
        QueryWrapper<Xktable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userid);
        queryWrapper.eq("course_id",couresid);
        xktableService.remove(queryWrapper);
        return "ok";
    }
    //选课信息搜索-根据用户id
    @GetMapping("search")
    @ResponseBody
    public List<Xktableadmin> search(@RequestParam Integer searchstr, HttpSession session){
        List<Xktable> searcList = xktableService.xktablelikeLise(searchstr);
        List<Xktableadmin> xktableadmins = new ArrayList<>();
        if (!ObjectUtils.isEmpty(searchstr)){
            for (Xktable xk : searcList) {
                Syuser syuser = syuserService.getById(xk.getUserId());
                Course course = courseService.getById(xk.getCourseId());
                Xktableadmin xktableadmin = new Xktableadmin();
                xktableadmin.setCourseId(course.getCourseId());
                xktableadmin.setUserId(syuser.getUserId());
                xktableadmin.setUserName(syuser.getUserName());
                xktableadmin.setCourseName(course.getCourseName());
                xktableadmin.setCourseDetails(course.getCourseDetails());
                xktableadmins.add(xktableadmin);
            }
        }
        return xktableadmins;
    }

}

